package controllers;

import java.util.List;

import play.mvc.Controller;
import models.Product;
import models.ProductType;

public class ProductTypes extends Controller {
	/**
	 * 添加客户
	 * 
	 * @param productType
	 */
	public static void addProductType() {
		render();	
	}
	public static void saveProductType(ProductType producttype) {
		producttype.save();
		listProductType();		
	}

	/**
	 * 修改客户
	 * 
	 * @param productTypeID
	 */
	public static void editProductType(long productTypeID) {
		ProductType producttype = ProductType.find("id=?", productTypeID).first();
		render(producttype);
	}

	/**
	 * 删除客户
	 * 
	 * @param productTypeID
	 */
	public static void deleteProductType(long productTypeID) {
		ProductType productType = ProductType.find("id=?", productTypeID).first();
		List<Product> products = null;
		if(productType!=null)
		{
			products = Product.find("producttype_id = ?",productType.id).fetch();
			for(int i=0;i<products.size();i++){
				products.get(i).delete();
			}
		}
		productType.delete();
		listProductType();	
	}
	
	/**
	 * 客户列表
	 */
	public static void listProductType(){
		List<ProductType> productTypeList = ProductType.findAll();
        render(productTypeList);   
	}
	
	public static void search(String searchText){
		List<ProductType> productTypeList = ProductType.find("producttypename = ?",searchText).fetch();
		render(productTypeList);
	}

}
